import os
import shutil
from app.core.config import settings

class FileService:
    """文件服务类，处理文件和目录操作"""
    
    @staticmethod
    def delete_task_files(task_id: str, client_id: str,task_type:str="DOI") -> bool:
        """删除与特定任务相关的所有文件
        
        Args:
            task_id: 任务ID
            client_id: 客户端ID
            
        Returns:
            bool: 操作是否成功
        """
        try:
            # 删除任务专用下载目录
            client_dir = os.path.join(settings.DOWNLOAD_DIR, client_id,task_type)
            download_dir = os.path.join(client_dir, task_id)
            if os.path.exists(download_dir) and os.path.isdir(download_dir):
                shutil.rmtree(download_dir)
                print(f"任务下载目录删除成功: {download_dir}")
            
            # 删除ZIP压缩包
            zip_path = os.path.join(client_dir, f"{task_id}.zip")
            if os.path.exists(zip_path):
                os.remove(zip_path)
                print(f"任务ZIP文件删除成功: {zip_path}")
                
            # 如果client_dir为空，也删除它
            if os.path.exists(client_dir) and os.path.isdir(client_dir) and not os.listdir(client_dir):
                os.rmdir(client_dir)
                print(f"客户端下载目录为空，已删除: {client_dir}")
            
            # 删除源文件目录
            upload_client_dir = os.path.join(settings.UPLOAD_DIR, client_id)
            upload_dir = os.path.join(upload_client_dir, task_id)
            if os.path.exists(upload_dir) and os.path.isdir(upload_dir):
                shutil.rmtree(upload_dir)
                print(f"任务上传目录删除成功: {upload_dir}")
                
            # 如果upload_client_dir为空，也删除它
            if os.path.exists(upload_client_dir) and os.path.isdir(upload_client_dir) and not os.listdir(upload_client_dir):
                os.rmdir(upload_client_dir)
                print(f"客户端上传目录为空，已删除: {upload_client_dir}")
                
            return True
        except Exception as e:
            print(f"删除任务文件失败: {str(e)}")
            return False
    
    @staticmethod
    def delete_client_files(client_id: str,task_type:str="DOI") -> bool:
        """删除与特定客户端相关的所有文件
        
        Args:
            client_id: 客户端ID
            
        Returns:
            bool: 操作是否成功
        """
        try:
            # 删除下载目录
            client_dir = os.path.join(settings.DOWNLOAD_DIR, client_id,task_type)
            if os.path.exists(client_dir) and os.path.isdir(client_dir):
                shutil.rmtree(client_dir)
                print(f"客户端下载目录删除成功: {client_dir}")
            
            # 删除上传目录
            upload_client_dir = os.path.join(settings.UPLOAD_DIR, client_id,task_type)
            if os.path.exists(upload_client_dir) and os.path.isdir(upload_client_dir):
                shutil.rmtree(upload_client_dir)
                print(f"客户端上传目录删除成功: {upload_client_dir}")
                
            return True
        except Exception as e:
            print(f"删除客户端目录失败: {str(e)}")
            return False
    
 

# 创建全局文件服务实例
file_service = FileService()